"""
Factory for creating embedder instances based on configuration.
"""

from typing import Optional
from .base import IEmbedder
from .openai import OpenAiEmbedder
from .ollama import OllamaEmbedder
from ..config import EmbedderConfig


class EmbedderFactory:
    """Factory for creating embedder instances based on configuration."""
    
    @staticmethod
    def create_embedder(config: EmbedderConfig) -> IEmbedder:
        """Create an embedder instance based on the configuration."""
        
        if config.provider == "openai":
            if not config.options.get("api_key"):
                raise ValueError("OpenAI API key is required")
            return OpenAiEmbedder(
                api_key=config.options["api_key"],
                model_id=config.options.get("model_id", "text-embedding-3-small")
            )
            
        elif config.provider == "ollama":
            if not config.options.get("base_url"):
                raise ValueError("Ollama base URL is required")
            return OllamaEmbedder(
                base_url=config.options["base_url"],
                model_id=config.options.get("model_id", "bge-m3:latest")
            )
            
        elif config.provider == "openai-compatible":
            if not config.options.get("base_url") or not config.options.get("api_key"):
                raise ValueError("OpenAI-compatible base URL and API key are required")
            # Create OpenAI-compatible embedder using OpenAI client with custom base URL
            # This would require custom OpenAI client configuration
            # For now, use basic OpenAI embedder (would need enhancement)
            return OpenAiEmbedder(
                api_key=config.options["api_key"],
                model_id=config.options.get("model_id", "text-embedding-ada-002")
            )
            
        elif config.provider == "gemini":
            # TODO: Implement Gemini embedder
            raise NotImplementedError("Gemini embedder not implemented yet")
            
        elif config.provider == "mistral":
            # TODO: Implement Mistral embedder
            raise NotImplementedError("Mistral embedder not implemented yet")
            
        else:
            raise ValueError(f"Unsupported embedder provider: {config.provider}")
    
    @staticmethod
    def get_vector_dimensions(config: Optional[EmbedderConfig] = None) -> dict[str, int]:
        """Gets the vector dimensions (same for all document types)."""
        # Default dimensions - this could be made configurable
        dimension = 4096
        return {
            "code": dimension,
            "text": dimension,
        }